#!/usr/bin/env python
# -*- coding: utf-8 -*-
# import os,re
# os.environ['DJANGO_SETTINGS_MODULE'] = 'drf2.settings'
# import django
# django.setup()
import time
import configparser
from django.conf import settings
import asyncio,aiohttp

async def check_url(name,url):
    async with aiohttp.ClientSession() as session:
        async with await session.get(url) as r:
            if r.status == 200:
                print('%s  %s ok' % (name, url))
            else:
                print('%s  %s fail' % (name, url))

def main():
    start_time = time.time()
    urls_state = []
    cfg = configparser.ConfigParser()
    cfg.read('d:/drf2/conf/config.cfg',encoding='utf-8')
    # print('网页访问:')
    urls = cfg.items('url')
    # urls = ['http://www.umlink.cn/index.hm', 'http://www.gausstel.com/']
    tasks = []

    for url in urls:
        c = check_url(url[0],url[1])
        task = asyncio.ensure_future(c)
        tasks.append(task)

    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.wait(tasks))
    end_time = time.time()
    print(end_time - start_time)
main()

